package com.heu.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heu.reggie.common.R;
import com.heu.reggie.entity.Orders;
import com.heu.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author: lidong
 * @description
 * @date: 2022/11/10 11:59
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
//        Long id = orders.getId();
        orderService.submit(orders);
        return R.success("下单成功");

    }

    /**
     * 订单的分页
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTme
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize ,String number , String beginTime , String endTme){
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(number!=null, Orders::getNumber , number)
                .gt(StringUtils.isNotEmpty(beginTime),Orders::getOrderTime,beginTime)
                .gt(StringUtils.isNotEmpty(endTme),Orders::getCheckoutTime , endTme);
        orderService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    @GetMapping("/userPage")
    public R<Page> page(int page , int pageSize){
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Orders::getOrderTime);
        orderService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }




    @PutMapping
    public R<String> orderStatusChange(@RequestBody Map<String,String> map){
        String id = map.get("id");
        Long orderId = Long.parseLong(id);

        Integer status = Integer.parseInt(map.get("status"));

        if (orderId == null || status== null){
            return R.error("请求参数异常");
        }

        Orders orders = orderService.getById(orderId);
        orders.setStatus(status);
        orderService.updateById(orders);

        return R.success("订单状态修改成功");

    }
}
